##########################################################################
#  Copyright (C) 2007 Robert Connolly <rob_connolly@mailshack.com>       #
#                                                                        #
#  This program is free software; you can redistribute it and/or modify  #
#  it under the terms of the GNU General Public License as published by  #
#  the Free Software Foundation; either version 3 of the License, or     #
#  (at your option) any later version. See file COPYING for details.     #
#                                                                        #
#  This program is distributed in the hope that it will be useful,       #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#  GNU General Public License for more details.                          #
#                                                                        #
#  You should have received a copy of the GNU General Public License     #
#  along with this program; if not, write to the                         #
#  Free Software Foundation, Inc.,                                       #
#  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
##########################################################################

import gtk, gtk.glade
import gconf
import cairo
import rsvg

from cairolets.core import Display
from cairolets.sensors.ClockSensor import ClockSensor

class DigitalClockPrefsDialog():
	
	def __init__(self):
		self.widgets = gtk.glade.XML('/home/robert/Desktop/cairolets/displays/DigitalClock/prefs-dialog.glade', "DigitalClockPrefs")
		
		client = gconf.client_get_default()
		
		if client.get_bool("/apps/cairolets/displays/DigitalClock/twelve_hour"):
			rdo = self.widgets.get_widget("rdo12hr")
		else:
			rdo = self.widgets.get_widget("rdo24hr")
		rdo.set_active(True)
		
		chkSeconds = self.widgets.get_widget("chkSeconds")
		chkSeconds.set_active(client.get_bool("/apps/cairolets/displays/DigitalClock/show_seconds"))
		
		chkDate = self.widgets.get_widget("chkDate")
		chkDate.set_active(client.get_bool("/apps/cairolets/displays/DigitalClock/show_date"))
		
		dialog = self.widgets.get_widget("DigitalClockPrefs")
		dialog.set_title("Digital Clock")
		
		dic = {"on_btnClose_clicked": self.btnClose_clicked, "on_btnHelp_clicked": self.btnHelp_clicked}
	        self.widgets.signal_autoconnect(dic)
		
		dialog.run()
	        dialog.destroy()
	        
	def btnClose_clicked(self, widget):
		client = gconf.client_get_default()
		
		rdo = self.widgets.get_widget("rdo12hr")
		client.set_bool("/apps/cairolets/displays/DigitalClock/twelve_hour", rdo.get_active())
	
		chkSeconds = self.widgets.get_widget("chkSeconds")
		client.set_bool("/apps/cairolets/displays/DigitalClock/show_seconds", chkSeconds.get_active())
		
		chkDate = self.widgets.get_widget("chkDate")
		client.set_bool("/apps/cairolets/displays/DigitalClock/show_date", chkDate.get_active())
	
		dialog = self.widgets.get_widget("DigitalClockPrefs")
		dialog.response(gtk.RESPONSE_OK)
		
	def btnHelp_clicked(self, widget):
		pass

class DigitalClock(Display):
	"""<name>DigitalClock</name>
	   <description>A Very Simple Digital Clock Display</description>
	   <author email='rob_connolly@mailshack.com'>Robert Connolly</author>
	   <website>http://cairolets.googlecode.com</website>"""
	
	def __init__(self, name):
		Display.__init__(self, name, True)
		
		self.sensor = ClockSensor()
		self.sensor.connect('data-changed', self.sensor_update)
		self.sensor_update(self.sensor, "second")
		
		self.svg = rsvg.Handle("/home/robert/Desktop/cairolets/displays/DigitalClock/DigitalClock.svg")
		
	def do_expose(self, ctx):
		self.resize(self.svg.get_property('width'), self.svg.get_property('height'))
		self.svg.render_cairo(ctx)
		
		ctx.set_source_rgba(0.33, 0.31, 0.31, 0.80)
		ctx.select_font_face("Bitstream Vera Sans Mono", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
		
		ctx.set_font_size(24)
		if self.client.get_bool("/apps/cairolets/displays/DigitalClock/show_seconds"):
			if self.client.get_bool("/apps/cairolets/displays/DigitalClock/twelve_hour"):
				ctx.set_font_size(16)
		
		xbearing, ybearing, width, height, xadvance, yadvance = (ctx.text_extents(self.time))
		x = (self.svg.get_property('width')/2) - (width/2) - 3
		if self.client.get_bool("/apps/cairolets/displays/DigitalClock/show_date"):
			y = height + 10
		else:
			y = height + 20
		ctx.move_to(x, y)
		ctx.show_text(self.time)
		
		ctx.set_font_size(16)
		
		if self.client.get_bool("/apps/cairolets/displays/DigitalClock/show_date"):
			xbearing, ybearing, width, height, xadvance, yadvance = (ctx.text_extents(self.date))
			x = (self.svg.get_property('width')/2) - (width/2) - 3
			y = height + 35
			ctx.move_to(x, y)
			ctx.show_text(self.date)
		
	def do_window_mask(self):
		self.svg.render_cairo(self.mask)
		
	def do_prefs_dialog(self):
		DigitalClockPrefsDialog()
		
	def do_quit(self):
		self.sensor.kill()
		
	def sensor_update(self, sensor, key):
		data  = sensor.get_data()
		
		if data['hour'] < 10:
			hour = '0%i'  % data['hour']
		else:
			hour = str(data['hour'])
		if data['minute'] < 10:
			minute = '0%i'  % data['minute']
		else:
			minute = str(data['minute'])
		if data['second'] < 10:
			second = '0%i'  % data['second']
		else:
			second = str(data['second'])
			
		if self.client.get_bool("/apps/cairolets/displays/DigitalClock/twelve_hour"):
			if data['hour'] > 12:
				h = data['hour'] - 12
				if h < 10:
					hour = '0%i' % h
				else:
					hour = str(h)
				if hour == '12':
					ampm = "AM"
				else:
					ampm = "PM"
			else:
				if hour == '12':
					ampm = "PM"
				else:
					ampm = "AM"
		
		if self.client.get_bool("/apps/cairolets/displays/DigitalClock/show_seconds"):
			if self.client.get_bool("/apps/cairolets/displays/DigitalClock/twelve_hour"):
				self.time = "%(h)s:%(m)s:%(s)s %(a)s" % {'h': hour, 'm': minute, 's': second, 'a': ampm}
			else:
				self.time = "%(h)s:%(m)s:%(s)s" % {'h': hour, 'm': minute, 's': second}
		else:
			if self.client.get_bool("/apps/cairolets/displays/DigitalClock/twelve_hour"):
				self.time = "%(h)s:%(m)s %(a)s" % {'h': hour, 'm': minute, 'a': ampm}
			else:
				self.time = "%(h)s:%(m)s" % {'h': hour, 'm': minute}
		
		if data['day'] < 10:
			day = '0%i'  % data['day']
		else:
			day = str(data['day'])
		if data['month'] < 10:
			month = '0%i'  % data['month']
		else:
			month = str(data['month'])
		year = str(data['year'])
			
		self.date = "%(d)s/%(m)s/%(y)s" % {'d': day, 'm': month, 'y': year}
		
		self.queue_draw()
		
